"use strict"
;(function($, window, document, undefined) {
	//初始化图表对象变量
	var myChart1,
		myChart2,
		myChart3,
		myChart4;
	
	$.extend({
		//init chart
		initAllChart:function(){
			myChart1 = echarts.init(document.getElementById('beLate'));
			myChart2 = echarts.init(document.getElementById('leaveEarly'));
			myChart3 = echarts.init(document.getElementById('absenteeism'));
			myChart4 = echarts.init(document.getElementById('onSchool'));
			
			myChart1.setOption(beLateOption);//迟到
			myChart2.setOption(leaveEarlyOption);//早退
			myChart3.setOption(absenteeismOption);//旷工
			myChart4.setOption(onSchoolOption);//在校
			 
		},
		//拖拽布局
		layoutTable:function(){
			$( "#layoutContainer" ).sortable({
		      connectWith: "#layoutContainer",
		      handle: ".portlet-title",//指定拖拽的选择器
		      cancel: ".collapse,.remove,.reload",//禁用拖拽的选择器
		    });
		 
		},
		//获取个性化配置数据
		getConfing:function(){
			var aForm = $("#diyForm").serializeArray();
			var oMain = {
				layout:"",
				module:[]
			};
			$.each(aForm, function(i,v) {
				if(v.name == "module"){
					oMain.module.push(v.value);
				}
				if(v.name == "layout"){
					oMain.layout = v.value;
				}
			});
			
			return oMain;
		},
		/*
		 渲染个性化的布局
		 @param oLayoutMsg {object} 个性化定制数据配置
		 * */
		layoutLoad:function(oLayoutMsg){
			$("#layoutContainer>div").each(function(){
				this.classList = oLayoutMsg.layout + " col-xs-12";
				$(this).addClass("hide");
				$.each(oLayoutMsg.module, function(k,e) {
					$("#"+e).removeClass("hide");
				});
			});
					
			myChart1 = echarts.init(document.getElementById('beLate'));
			myChart2 = echarts.init(document.getElementById('leaveEarly'));
			myChart3 = echarts.init(document.getElementById('absenteeism'));
			myChart4 = echarts.init(document.getElementById('onSchool'));
			
			myChart1.setOption(beLateOption);//迟到
			myChart2.setOption(leaveEarlyOption);//早退
			myChart3.setOption(absenteeismOption);//旷工
			myChart4.setOption(onSchoolOption);//在校
			
		    //回显配置开关
		    $("input[value="+oLayoutMsg.layout+"]").prop("checked",true).parent().siblings("label").find("input[type=radio]").prop("checked",false);
		    $("#moduleBox input[type=checkbox]").prop("checked",false);
		    $.each(oLayoutMsg.module, function(i,v) {
		    	$("input[value="+v+"]").prop("checked",true);
		    });
		},
		//个性化定制,初始化个性化配置，保存配置
		Diy:function(){
			
			//初始化工作台布局和功能配置
			var layoutMsg = {
				layout:"col-sm-6",
				module:[
					"beLateBox",
					"leaveEarlyBox",
					"absenteeismBox",
					"onSchoolBox"
				]
			};
			//已经定制的布局
			if(typeof JSON.parse(localStorage.getItem("layoutMsg")) == "object"){
				layoutMsg = JSON.parse(localStorage.getItem("layoutMsg"));
				console.log(layoutMsg);
				$.layoutLoad(layoutMsg);	
			}
			else{
				$.initAllChart();
			}
			//配置改变监听
			$("#Diy input[type=checkbox],#Diy input[type=radio]").on("change",function(){
				var oMain = $.getConfing();
				console.log(oMain);
				$.layoutLoad(oMain);
				
			});
			//点击保存
			$(".btn-save-confing").on("click",function(){
				var oMain = $.getConfing();
				var userMsg = JSON.parse(localStorage.getItem("userMsg"));//存用户信息
				oMain.uid = userMsg.id;
				//后台请求
				fnAjax.method_4(
					url_join("free/layout"),
					oMain,
					"post",
					function(data){
						console.log(data);
						layer.msg("操作成功",{time:1000},function(){
							var lm = JSON.stringify(oMain);
							localStorage.setItem("layoutMsg",lm);
							$("#layoutOption").modal("toggle");
						});
					}
				);
			});
			
			//浏览器大小改变时重置大小
			$(window).on("resize",function(){
				myChart1.resize();
				myChart2.resize();
				myChart3.resize();
			    myChart4.resize();
			});
			
			$("#Diy input[type=radio]").change(function(){
				console.log("布局改变");
				setTimeout(function(){
					myChart1.resize();
					myChart2.resize();
					myChart3.resize();
				    myChart4.resize();
				},800);
				
			});
			
		},		
		//获取皮肤样式       
		getStyle_color:function(){
			console.log($.cookie('style_color'));
			if($.cookie('style_color')){
				$(".portlet").each(function(i){
					this.classList = "portlet box "+$.cookie('style_color');
				});
			}
			else{
				$(".portlet").each(function(i){
					this.classList = "portlet box default";
				});
			}
		}
	});
})(jQuery, window, document);

 